package com.web.frame.controller;

import com.web.frame.mysql.UserLoginRegister;
import com.web.frame.user.NewHtmlFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    Logger logger = LoggerFactory.getLogger(UserLoginRegister.class);
    @Autowired
    UserLoginRegister userLoginRegister;

    @RequestMapping("/login")
    public String log(){
        return "login";
    }

    @RequestMapping(value = "/loginIn",method = RequestMethod.POST)
    public String login(String name, String password, HttpServletResponse response){
        String loginMessage;
        loginMessage = userLoginRegister.login(name, password);
        if("success".equals(loginMessage)){
            logger.info(name + "登录成功!");
            Cookie cookie = new Cookie("SUCCESS", name);
            cookie.setPath("/");
            cookie.setMaxAge(2592000);
            response.addCookie(cookie);
            return "lsuccess";
        }else if ("noSuccess".equals(loginMessage)){
            logger.info(name + "登录不成功!账号密码错误");
            return "lerror";
        }else {
            logger.info("代码错误");
            return "codeerr";
        }
    }

    @RequestMapping("/register")
    public String reg(){
        return "register";
    }

    @RequestMapping(value = "/registerIn",method = RequestMethod.POST)
    public String register(String name,String password, HttpServletResponse response){
        NewHtmlFile newHtmlFile = new NewHtmlFile();
        String registerMessage;
        registerMessage = userLoginRegister.register(name, password);
        if ("success".equals(registerMessage)) {
            newHtmlFile.newFile(name);
            logger.info(name + "注册成功!");
            Cookie cookie = new Cookie("SUCCESS", name);
            cookie.setPath("/");
            cookie.setMaxAge(2592000);
            response.addCookie(cookie);
            return "rsuccess";
        } else if ("noSuccess".equals(registerMessage)){
            logger.info(name + "注册不成功!有同名用户!");
            return "rerror";
        }else {
            logger.info("代码错误");
            return "codeerr";
        }
    }

    @RequestMapping("/lll")
    @ResponseBody
    public String testMapping(){
        System.out.println("world");
        return "hello world";
    }
    /*
    @RequestMapping("/content/user/toDeleteUser")
    public String toDelete(){
        return "content/user/toDeleteUser";
    }

    @RequestMapping("/content/user/deleteUser")
    public String delete(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String session = null;
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("SUCCESS")) {
                    session = cookie.getValue();
                }
            }
        }
        String deleteUserMessage;
        deleteUserMessage = userLoginRegister.deleteUser(session);
        if ("success".equals(deleteUserMessage)) {
            logger.info("用户:" + session + "删除成功!");
            return "content/user/deleteUserSuccess";
        } else if ("noSuccess".equals(deleteUserMessage)){
            logger.info("用户:" + session + "删除失败");
            return "content/user/deleteUserError";
        }else {
            logger.info("代码错误");
            return "content/user/deleteUserError";
        }
    }
     */
}
